<!DOCTYPE HTML>
<html lang="en">
<head>
<title>IfEqual / IfLess / IfGreater - Syntax &amp; Usage | AutoHotkey</title>
<meta name="description" content="The IfEqual, IfNotEqual, IfLess, IfLessOrEqual, IfGreater and IfGreaterOrEqual statements specify statements to execute if the comparison evaluates to true." />
<meta name="ahk:equiv-v2" content="commands/IfExpression.htm" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>If / IfEqual / IfNotEqual / IfLess / IfLessOrEqual / IfGreater / IfGreaterOrEqual</h1>
<p>Specifies one or more <a href="../Concepts.htm#statement">statements</a> to execute if the comparison of a <a href="../Variables.htm">variable</a> to a value evaluates to true.</p>
<p class="warning"><strong>Deprecated:</strong> Legacy If statements are not recommended for use in new scripts. See <a href="../Language.htm#if-statement">Scripting Language: If Statement</a> for details and use <a href="IfExpression.htm">If (expression)</a> instead.</p>
<pre class="Syntax">
<span class="func">IfEqual</span>, Var <span class="optional">, Value</span>          <em>; if Var = Value</em>
<span class="func">IfNotEqual</span>, Var <span class="optional">, Value</span>       <em>; if Var != Value</em>
<span class="func">IfLess</span>, Var <span class="optional">, Value</span>           <em>; if Var &lt; Value</em>
<span class="func">IfLessOrEqual</span>, Var <span class="optional">, Value</span>    <em>; if Var &lt;= Value</em>
<span class="func">IfGreater</span>, Var <span class="optional">, Value</span>        <em>; if Var &gt; Value</em>
<span class="func">IfGreaterOrEqual</span>, Var <span class="optional">, Value</span> <em>; if Var &gt;= Value</em>
</pre>

<h2>Parameters</h2>
<dl>
  <dt>Var</dt>
  <dd>The name of a <a href="../Variables.htm">variable</a>. Percent signs must be omitted except when attempting a <a href="../Language.htm#dynamic-variables">double reference</a>. Unlike the input variables of other commands, the <a href="../Language.htm#-expression">percent prefix</a> is not supported.</dd>
  <dt>Value</dt>
  <dd><a href="../Language.htm#unquoted-text">Unquoted text</a> or a <a href="../Concepts.htm#numbers">number</a>. Variable references must be enclosed in percent signs (e.g. %var2%). <em>Value</em> can be omitted if you wish to compare <em>Var</em> to an empty string (blank).</dd>
</dl>

<h2>Remarks</h2>
<p>If both <em>Var</em> and <em>Value</em> are purely numeric, they will be compared as numbers rather than as strings. Otherwise, they will be compared alphabetically as strings (that is, alphabetical order will determine whether <em>Var</em> is greater, equal, or less than <em>Value</em>).</p>
<p>If an <em>If</em> owns more than one line, those lines must be enclosed in braces (to create a <a href="Block.htm">block</a>). However, if only one line belongs to an <em>If</em>, the braces are optional. For example:</p>
<pre>if count &lt;= 0
{
    WinClose Untitled - Notepad
    MsgBox There are no items present.
}</pre>
<p>Note that command-like If statements allow a <a href="../Language.htm#commands">command</a> or command-like <a href="../Language.htm#control-flow">control flow statement</a> to be written on the same line, but mispelled command names are treated as literal text. In other words, these are valid:</p>
<pre>IfEqual, x, 1, Sleep, 1
IfGreater, x, 1, EnvAdd, x, 2</pre>
<p>But these are not valid:</p>
<pre>if x = 1 Sleep 1
IfGreater, x, 1, x += 2</pre>
<p>The One True Brace (OTB) style may <strong>not</strong> be used with legacy If statements. It can only be used with <a href="IfExpression.htm">If (expression)</a>.</p>
<p>On a related note, the statement <code><a href="IfBetween.htm">if Var between LowerBound and UpperBound</a></code> checks whether a variable is between two values, and <code><a href="IfIn.htm">if Var in MatchList</a></code> can be used to check whether a variable's contents exist within a list of values.</p>

<h2>Related</h2>
<p><a href="IfExpression.htm">If (expression)</a>, <a href="StringCaseSense.htm">StringCaseSense</a>, <a href="SetExpression.htm">Assign expression (:=)</a>, <a href="IfIn.htm">if var in/contains</a>, <a href="IfBetween.htm">if var between</a>, <a href="IfInString.htm">IfInString</a>, <a href="Block.htm">Blocks</a>, <a href="Else.htm">Else</a></p>

<h2>Examples</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a></p>
<pre>if counter &gt;= 1
    Sleep, 10

if counter &gt;=1   <em>; For executing more than one line, enclose those lines in braces:</em>
{
    WinClose, Untitled - Notepad
    Sleep 10
}

if MyVar = %MyVar2%
    MsgBox The contents of MyVar and MyVar2 are identical.
else if MyVar =
{
    MsgBox, 4,, MyVar is empty/blank. Continue?
    IfMsgBox, No
        Return
}
else if MyVar != ,
    MsgBox The value in MyVar is not a comma.
else
    MsgBox The value in MyVar is a comma.

if Done
    MsgBox The variable Done is neither empty nor zero.</pre>
</div>

</body>
</html>
